import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:steam_app/config/app_theme.dart';
import '../../entity/game.dart';
import '../../entity/product.dart';
import '../../provider/game_detail_provider.dart';
import '../../provider/user_provider.dart';
import '../widgets/product_list_item.dart'; // 我们将创建一个新组件

class GameDetailPage extends StatelessWidget {
  const GameDetailPage({super.key});

  @override
  Widget build(BuildContext context) {
    // 从路由参数中获取初始的 Game 对象
    final initialGame = ModalRoute.of(context)!.settings.arguments as Game;
    final user = context.watch<UserProvider>().currentUser!;

    // 使用 ChangeNotifierProvider 为此页面创建一个独立的 Provider 实例
    return ChangeNotifierProvider(
      create: (context) => GameDetailProvider()
        ..loadGameDetails(initialGame: initialGame, userId: user.id),
      child: Consumer<GameDetailProvider>(
        builder: (context, provider, child) {
          return Scaffold(
            body: provider.gameDetailData == null
                ? const Center(child: CircularProgressIndicator())
                : _buildContent(context, provider),
          );
        },
      ),
    );
  }

  Widget _buildContent(BuildContext context, GameDetailProvider provider) {
    final gameData = provider.gameDetailData!;
    final game = gameData.game;

    return CustomScrollView(
      slivers: [
        // 1. 可伸缩的顶部 AppBar，显示游戏封面
        SliverAppBar(
          expandedHeight: 250.0,
          floating: false,
          pinned: true,
          flexibleSpace: FlexibleSpaceBar(
            title: Text(
              game.name,
              style: const TextStyle(shadows: [Shadow(blurRadius: 8)]),
            ),
            background: Image.asset(
              game.coverImage,
              fit: BoxFit.cover,
            ),
          ),
        ),
        // 2. 页面的主要内容
        SliverToBoxAdapter(
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // 游戏描述
                Text(game.description, style: const TextStyle(fontSize: 16, height: 1.5)),
                const SizedBox(height: 16),
                Text('开发商: ${game.developer}'),
                const Divider(height: 32),

                // 操作按钮行
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    ElevatedButton.icon(
                      onPressed: () => provider.toggleWishlist(context.read<UserProvider>().currentUser!.id),
                      icon: Icon(
                        gameData.isInWishlist ? Icons.check_circle : Icons.favorite_border,
                        color: gameData.isInWishlist ? greenButtonColor : null,
                      ),
                      label: Text(gameData.isInWishlist ? '已在愿望单' : '加入愿望单'),
                    ),
                    ElevatedButton.icon(
                      onPressed: () {},
                      icon: const Icon(Icons.share),
                      label: const Text('分享'),
                    ),
                  ],
                ),
                const Divider(height: 32),

                // 购买选项标题
                const Text('购买此游戏', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
                const SizedBox(height: 16),
              ],
            ),
          ),
        ),

        // 3. 将产品列表渲染为 SliverList
        SliverList(
          delegate: SliverChildBuilderDelegate(
                (context, index) {
              final product = game.products[index];
              return ProductListItem(product: product);
            },
            childCount: game.products.length,
          ),
        ),
      ],
    );
  }
}